package com.cn.transfer.common.http;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.apache.http.Header;


public class ApiClient {


    /**
     * 发送POST请求
     *
     * @param headers 请求头部
     * @param json    请求体
     * @return
     */
    public static JSONObject sendPostRequest(String url, Header[] headers, String json) {
        HttpRequest post = HttpRequest.post(url);
        for (Header header : headers) {
            post.header(header.getName(), header.getValue());
        }
        HttpResponse execute = post.body(json).execute();
        int status = execute.getStatus();
        if (status != 200) {
            for (int i = 0; i < 10; i++) {
                execute = post.body(json).execute();
                int statusi = execute.getStatus();
                if (statusi == 200) {
                    i = 10;
                }
            }
        }
        String body = execute.body();
        return JSONUtil.parseObj(body);
    }
    /**
     * 发送GET请求
     *
     * @param headers 请求头部
     * @return 返回的JSON对象
     */
    public static JSONObject sendGetRequest(String url, Header[] headers) {
        HttpRequest get = HttpRequest.get(url);
        for (Header header : headers) {
            get.header(header.getName(), header.getValue());
        }
        HttpResponse execute = get.execute();
        int status = execute.getStatus();
        if (status != 200) {
            for (int i = 0; i < 10; i++) {
                execute = get.execute();
                int statusi = execute.getStatus();
                if (statusi == 200) {
                    i = 10;
                }
            }
        }
        String body = execute.body();
        return JSONUtil.parseObj(body);
    }

    public static void sendPostRequestTpuls(String url, Header[] headers, String json) {
        HttpRequest post = HttpRequest.post(url);
        for (Header header : headers) {
            post.header(header.getName(), header.getValue());
        }
        String body = post.body(json).execute().body();
    }

    public static JSONObject sendGetRequest(String url, Header[] headers, String json) {
        HttpRequest get = HttpRequest.get(url);
        for (Header header : headers) {
            get.header(header.getName(), header.getValue());
        }

        System.out.println("Request URL: " + url);
        for (Header header : headers) {
            System.out.println("Header: " + header.getName() + ": " + header.getValue());
        }

        try {
            HttpResponse execute = get.execute();
            int status = execute.getStatus();
            System.out.println("Response Status: " + status);

            if (status != 200) {
                for (int i = 0; i < 10; i++) {
                    execute = get.body(json).execute();
                    int statusi = execute.getStatus();
                    if (statusi == 200) {
                        break; // Exit the loop if successful
                    }
                }
            }

            String body = execute.body();
            return JSONUtil.parseObj(body);
        } catch (Exception e) {
            e.printStackTrace(); // Print the full stack trace for debugging
        }
        return null; // Or handle the error accordingly
    }



}